package com.right.health.app.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.magic.basic.activity.BaseFragmentActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;

import java.util.List;

/**
 * Base Presente Activity
 * <p>
 * Created by Hope on 17/3/1.
 */

public abstract class BasePresenterActivity<T extends IBasePresenter,K> extends BaseFragmentActivity
        implements IBaseView<K> {

    private T mPresenter;

    private List<IBasePresenter> mSelfPresenter;

    @Override
    public void setContentView(int layoutResID) {
        buildConfig();
        super.setContentView(layoutResID);
    }

    private void buildConfig() {
        mPresenter = createPresenter();
        mSelfPresenter = createSelfPresenter();

        if (mPresenter != null) {
            mPresenter.attachView(this);
        }

        if (getSelfPresenter() != null) {
            int size = getSelfPresenter().size();
            for (int i = 0; i < size; i++) {
                getSelfPresenter().get(i).attachView(this);
            }

        }
    }

    @Override
    public void setContentView(View view) {
        buildConfig();
        super.setContentView(view);

    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        buildConfig();
        super.setContentView(view, params);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getPersenter() != null) {
            getPersenter().onResume();
        }
        if (getSelfPresenter() != null) {
            int size = getSelfPresenter().size();
            for (int i = 0; i < size; i++) {
                getSelfPresenter().get(i).onResume();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (getPersenter() != null) {
            getPersenter().onPause();
        }
        if (getSelfPresenter() != null) {
            int size = getSelfPresenter().size();
            for (int i = 0; i < size; i++) {
                getSelfPresenter().get(i).onPause();
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (getPersenter() != null) {
            getPersenter().detachView();
        }
        if (getSelfPresenter() != null) {
            int size = getSelfPresenter().size();
            for (int i = 0; i < size; i++) {
                getSelfPresenter().get(i).detachView();
            }
            getSelfPresenter().clear();

        }
        super.onDestroy();
    }

    /**
     * 创建prensenter
     */
    public T createPresenter() {
        Class clazz = ClassUtil.getGenericClass(getClass());
        if (clazz != null) {
            try {
                return (T) clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public List<IBasePresenter> createSelfPresenter() {
        return null;
    }

    protected final List<IBasePresenter> getSelfPresenter() {
        if (CollectionUtil.isEmpty(mSelfPresenter)) {
            mSelfPresenter = createSelfPresenter();
        }
        return mSelfPresenter;
    }

    public T getPersenter() {
        if (mPresenter == null) {
            mPresenter = createPresenter();
        }

        return mPresenter;
    }


    @Override
    public Context getViewContext() {
        return this;
    }

    @Override
    public void showToast(String msg) {
        ToastUtil.show(this, msg);
    }

    @Override
    public void showToast(int msg) {
        ToastUtil.show(this, msg);
    }

    @Override
    public void loadError(int errorCode, String message) {

    }

    @Override
    public void hasMore(boolean hasMore) {

    }

    @Override
    public void loadComplete(List<K> dataSource) {

    }

    @Override
    public void loadComplete(K dataSource) {

    }
}
